﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ToolsLayer;
using DevExpress.XtraCharts;

namespace Webcar.UI
{
    public partial class SpecialChart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //.SearchClick += new EventHandler(SelectByMonth1_SearchClick);
            if (this.IsPostBack) return;
            this.TextBox1.Text = DateTime.Now.ToString("yyyy-MM");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet ds = DB.ExecuteDataset(string.Format(@"select c.short_name, c.dept_name,c.DEPT_CODE,a.nums total_nums,isnull(b.nums,0) nums from 
                                                            (
                                                            select dept_code,COUNT(1) nums from v_bs_car_record where car_class2='TZ' and IS_USE='T' 
                                                            group by dept_code
                                                            ) a
                                                            left join 
                                                            (
                                                            select  dept_code,COUNT(distinct car_no) nums from v_run_record where left(last_date_s,7) = '{0}'
                                                            group by dept_code) b
                                                            on a.dept_code = b.dept_code
                                                            join dept c
                                                            on a.dept_code=c.DEPT_CODE", this.TextBox1.Text));
            this.WebChartControl1.Series.Clear();
            //DataSet ds_dept = DB.ExecuteDataset("select * from dept");
            //添加Series
            Series series_study = new Series("已运行", ViewType.StackedBar);
            this.WebChartControl1.Series.Add(series_study);
            Series series_unstudy = new Series("未运行", ViewType.StackedBar);
            this.WebChartControl1.Series.Add(series_unstudy);
            series_unstudy.PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            series_study.PointOptions.ValueNumericOptions.Format = NumericFormat.FixedPoint;
            series_study.PointOptions.ValueNumericOptions.Precision = 1;
            series_unstudy.PointOptions.ValueNumericOptions.Precision = 1;
            //DataTable month_table = ds.Tables[0].DefaultView.ToTable(true, "ny");
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string dept_name = row["short_name"].ToString();
                if (string.IsNullOrEmpty(dept_name))
                {
                    dept_name = row["dept_name"].ToString();
                    if(dept_name.Length>2)
                        dept_name = dept_name.Substring(0,2);
                }
                int total_num = int.Parse(row["total_nums"].ToString());
                int study_num = int.Parse(row["nums"].ToString());

                SeriesPoint point_study = new SeriesPoint(dept_name,  study_num);
                //point_study.ArgumentSerializable = dept_name;
                series_study.Points.Add(point_study);

                SeriesPoint point_total = new SeriesPoint(dept_name, total_num - study_num);
                //point_total.ArgumentSerializable = dept_name;
                series_unstudy.Points.Add(point_total);
            }
        }
    }
}
